import Vue from 'vue'
import Router from '@/router'
import Message from './message.js'
import api from '@/api'
import Settings from '@/settings/projectSetting.js'
import Util from './test.js'
import EduWxpay from '@/components/EduWxpay'
import md5 from 'spark-md5'

// 使用extend方法创建vue的子类
const WxpayDialog = Vue.extend(EduWxpay)
const HangWxpay = new WxpayDialog()
const payment = {
  timer: null,
  // 法调用wxpay组件
  handlePaymentWxpay(options) {
    options = options || {}
    HangWxpay.wxpayQrcode = options.wxpayQrcode
    HangWxpay.wxpayVisible = options.wxpayVisible
    HangWxpay.vm = HangWxpay.$mount()
    // 实例化子组件，然后获取到DOM结构并挂载到body上
    document.body.appendChild(HangWxpay.vm.$el)
  },
  // 第三方支付状态查询
  handlePaymentOrderquery(order, pay, url, callbackSuccess) {
    this.timer = setInterval(() => {
      api.payOrderquery(
        {
          order_no: order,
          pay_type: pay
        },
        res => {
          if (res.data.result_state == 'SUCCESS') {
            clearInterval(this.timer) // 清除状态查询
            // 消息提示
            Message.success(res.data.result_msg, res => {
              HangWxpay.wxpayVisible = false
              Router.replace({
                path: url
              })
            })
            // 处理成功,是否定义了成功的处理方法
            if (Util.isFunction(callbackSuccess)) {
              return callbackSuccess(res.data)
            }
          }
        }
      )
    }, 5000)
  },
  /**
   * 支付方法
   * @param    {string}  order 订单号
   * @param    {string}  pay  支付方式 lcnpay：余额；alipay：支付宝；wxpay：微信；
   * @param    {string}  url  支付宝返回地址
   * */
  handlePaymentSubmit(order, pay, url, pwd, callbackSuccess, callbackError) {
    let query = ''
    if (pay == 'alipay') {
      query = {
        return_url: Settings.globLocalHost + url
      }
    }
    if (pay == 'lcnpay') {
      query = {
        password: md5.hash(pwd)
      }
    }
    api.setOrderPay(
      {
        order_no: order,
        pay_type: pay,
        payment_device: Settings.globDeviceType,
        ...query
      },
      res => {
        if (pay == 'alipay') {
          // 提交支付宝返回的form表单
          const div = document.createElement('div')
          div.innerHTML = res.data.paybody
          document.body.appendChild(div)
          document.forms['alipay_submit'].acceptCharset = 'UTF-8'
          document.forms['alipay_submit'].submit()
        } else if (pay == 'wxpay') {
          this.handlePaymentWxpay({
            wxpayQrcode: res.data.paybody.code_url,
            wxpayVisible: true
          })
          this.handlePaymentOrderquery(order, pay, url, callbackSuccess)
        } else if (pay == 'lcnpay') {
          Message.success(res.msg, () => {
            if (url) {
              Router.replace({
                path: url
              })
            }
          })
          // 处理成功,是否定义了成功的处理方法
          if (Util.isFunction(callbackSuccess)) {
            return callbackSuccess(res.data)
          }
        }
      },
      error => {
        Message.error(error.msg)
        // 处理错误
        if (Util.isFunction(callbackError)) {
          return callbackError(error.data)
        }
      }
    )
  }
}

export default payment
